package leetcode.Y2024M6.study;

import leetcode.Y2024M6.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class S637 {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> list = new ArrayList<>();
        if (root == null) return list;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            double sum=0;
            int count=size;
            while (size > 0) {
                TreeNode node = queue.poll();
                sum+= node.val;
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            list.add(sum / count);

        }
        return list;
    }

    @Test
    public void test() {
        int[] root={3,9,20,-1,-1,15,7};
        System.out.println(averageOfLevels(TreeNode.createTree(root)));
    }
}
